package model 
{
	import controller.EventBus;
	import controller.GameViewControllerEvent;
	import controller.NPCModelEvent;
	import model.data.InventoryObjectTypes;
	import model.data.NPCTypes;
	/**
	 * ...
	 * @author Gaz Williams
	 * 
	 * npc models are what the player interacts with, they hold an inventory item which is given to the player if
	 * the player supplies the npc with the correct item in turn
	 * 
	 */
	public class NPCModel 
	{
		private var _itemWanted:InventoryObjectTypes;
		public function get itemWanted():InventoryObjectTypes 
		{
			return _itemWanted;
		}
		
		private var _itemToGive:InventoryObjectTypes;
		public function get itemToGive():InventoryObjectTypes 
		{
			return _itemToGive;
		}
		
		private var whatNPC:NPCTypes;
		
		private var bus:EventBus;
		
		public function NPCModel(whatNPC:NPCTypes) 
		{
			
			this.whatNPC = whatNPC;
			
			switch (whatNPC.type) 
			{
				case NPCTypes.BROKEN_MONITOR:
					_itemWanted = new InventoryObjectTypes(InventoryObjectTypes.NOTHING);
					_itemToGive = new InventoryObjectTypes(InventoryObjectTypes.NOTHING);
				break;
				case NPCTypes.BROKEN_TABLE:
					_itemWanted = new InventoryObjectTypes(InventoryObjectTypes.GAFFER_TAPE);
					_itemToGive = new InventoryObjectTypes(InventoryObjectTypes.WOOD_SPLINTER);
				break;
				case NPCTypes.DEAD_RAT:
					_itemWanted = new InventoryObjectTypes(InventoryObjectTypes.NOTHING);
					_itemToGive = new InventoryObjectTypes(InventoryObjectTypes.DEAD_RAT);
				break;
				case NPCTypes.FRIDGE:
					_itemWanted = new InventoryObjectTypes(InventoryObjectTypes.FRIDGE_HANDLE);
					_itemToGive = new InventoryObjectTypes(InventoryObjectTypes.GAFFER_TAPE);
				break;
				case NPCTypes.MICROWAVE:
					_itemWanted = new InventoryObjectTypes(InventoryObjectTypes.PAPER_CUP_FILLED_WITH_SAND);
					_itemToGive = new InventoryObjectTypes(InventoryObjectTypes.MANAGERS_TROPHY);
				break;
				case NPCTypes.PRINTER:
					_itemWanted = new InventoryObjectTypes(InventoryObjectTypes.PAPER_CUP);
					_itemToGive = new InventoryObjectTypes(InventoryObjectTypes.PAPER_CUP_FILLED_WITH_SAND);
				break;
				case NPCTypes.TROPHY:
					_itemWanted = new InventoryObjectTypes(InventoryObjectTypes.MANAGERS_TROPHY);
					_itemToGive = new InventoryObjectTypes(InventoryObjectTypes.NOTHING); /*win game */
				break;
				case NPCTypes.WASTE_BIN:
					_itemWanted = new InventoryObjectTypes(InventoryObjectTypes.DEAD_RAT);
					_itemToGive = new InventoryObjectTypes(InventoryObjectTypes.FRIDGE_HANDLE);
				break;
				case NPCTypes.WATER_COOLER:
					_itemWanted = new InventoryObjectTypes(InventoryObjectTypes.WOOD_SPLINTER);
					_itemToGive = new InventoryObjectTypes(InventoryObjectTypes.PAPER_CUP);
				break;
			}
			
			bus = EventBus.getInstance();
			bus.addEventListener(GameViewControllerEvent.PLAYER_INTERACTED_WITH_NPC, handlePlayerInteraction);
		}
		
		public function dispose():void 
		{
			bus.removeEventListener(GameViewControllerEvent.PLAYER_INTERACTED_WITH_NPC, handlePlayerInteraction);
		}
		
		private function handlePlayerInteraction(e:GameViewControllerEvent):void 
		{
			var whichNPC:NPCTypes = e.data.npc as NPCTypes;
			if (whichNPC.type != whatNPC.type) return;
			
			var playerInventoryItem:InventoryObjectTypes = e.data.playerItem as InventoryObjectTypes;
			
			if (playerInventoryItem.type == itemWanted.type)
			{
				trace("right item");
				bus.dispatchEvent(new NPCModelEvent(NPCModelEvent.RIGHT_ITEM, itemToGive));
			}
			else
			{
				trace("wrong item");
				bus.dispatchEvent(new NPCModelEvent(NPCModelEvent.WRONG_ITEM, playerInventoryItem));
			}
			
		}
		
	}

}